package edu.ccut.saturn.basecomponent.databasecomponent;

import edu.ccut.saturn.basecomponent.database.IDeleteSQLBuilder;
import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.ISaturnSQLBuilder;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;

public class DeleteByPrimaryKey extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		debug(Messages.getString("DeleteByPrimaryKey.2"), parameterContext); //$NON-NLS-1$
		String fullKey = (String) parameterContext.getParameterValue(0);
		String primaryKeyValue = (String) parameterContext.getParameterValue(1);
		String dbType = SaturnDataUtil.getStandardDBType(fullKey);
		ISaturnSQLBuilder builder = SaturnSQLBuilderFactory
				.getSaturnSQLBuilder(dbType);
		if (builder != null) {
			String condition = SaturnDataUtil.getConditionWithPrimaryKeyByDbType(fullKey,
					primaryKeyValue);
			IDeleteSQLBuilder sqlBuilder = builder
					.getDeleteSQLBuilder(ISaturnSQL.DELETE_OPERATION);
			ISaturnSQL saturnSQL = sqlBuilder.build(fullKey, condition);
			int count = SaturnSQLHandler.executeUpdate(saturnSQL,
					parameterContext.getConnection());
			parameterContext.setParameter(2, count);
		}
	}

	

}
